package global.com.loansapplication.ui.actviity.user.presenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import global.com.loansapplication.base.BaseBean;
import global.com.loansapplication.base.BaseBiz;
import global.com.loansapplication.bean.ClueInfo;
import global.com.loansapplication.comm.Constants;
import global.com.loansapplication.comm.MyApplication;
import global.com.loansapplication.ui.actviity.user.ExportDataAct;
import global.com.loansapplication.ui.actviity.user.biz.OwnerBiz;
import global.com.loansapplication.ui.actviity.user.contract.OwnerContract;

public class OwnerPresenter implements OwnerContract.Presenter {

    private OwnerContract.View mView;
    private OwnerBiz mBiz;

    private int index = 1;
    private List<ClueInfo> clueInfoList = new ArrayList<>();

    @Inject
    public OwnerPresenter(OwnerContract.View view) {
        this.mView = view;
    }

    @Override
    public void setBiz(BaseBiz biz) {
        mBiz = (OwnerBiz) biz;
    }

    @Override
    public void getList() {
        mView.loading(true);
        mBiz.getClueList(MyApplication.userInfo.getUserCode(), index, Constants.PAGESIZE, new BaseBiz.Callback<BaseBean<List<ClueInfo>>>() {
            @Override
            public void onSuccess(BaseBean<List<ClueInfo>> o) {
                if (index == 1 && o.getList().size() == 0) {
                    mView.setNoDate(true);
                } else {
                    mView.setNoDate(false);
                }
                clueInfoList.addAll(o.getList());
                mView.updateCollection(clueInfoList);
                if (index > 1) {
                    mView.loadMoreComplete();
                } else {
                    mView.refreshComplete();
                }
                mView.loading(false);
                mView.hasLoadMore(index * Constants.PAGESIZE >= o.getTotal());
                index++;
            }

            @Override
            public void onFailure(BaseBean<List<ClueInfo>> o) {
                if (index > 1) {
                    mView.loadMoreComplete();
                } else {
                    mView.refreshComplete();
                }
                mView.loading(false);
            }
        });
    }

    @Override
    public void refresh() {
        index = 1;
        clueInfoList.clear();
        mView.updateCollection(clueInfoList);
        mView.hasLoadMore(false);
    }

    @Override
    public void toExportData() {
        mView.startAct(ExportDataAct.class,null);
    }

}
